#pragma once

#include <iostream>
#include <string>
#include <memory>
#include <sstream>
#include <jsoncpp/json/json.h>

const std::string Sep = "\r\n";

// 我们自己还有一次序列化，想要在json数据前加上“length/r/n
bool Encode(std::string &message)
{
    if (message.size() == 0)
        return false;

    std::string package = std::to_string(message.size()) + Sep + message + Sep;

    message = package;
    return true;
}

// 对我们自己定义的序列化反序列
bool Decode(std::string &package, std::string *content)
{
    

    auto pos = package.find(Sep);
    
    if (pos == std::string::npos)
        return false;

    
    std::string content_length_str = package.substr(0, pos);

    
    // 表示相应的报文长度

    int content_length = std::stoi(content_length_str);

    // 完整的报文长度，这里其实是不规范的，但是先这样了

    int full_length = content_length_str.size() + content_length + 2 * Sep.size();

    if (package.size() < full_length)
    {
        return false;
    }

    *content = package.substr(pos + Sep.size(), content_length);
    package.erase(0, full_length);
    return true;
}

//_x oper _y

// 定义相应的协议
class Request
{
private:
    int _x;
    char _oper;
    int _y;

public:
    Request() : _x(0), _y(0), _oper(0)
    {
    }
    Request(int x, int y, char oper) : _x(x), _y(y), _oper(oper)
    {
    }
    // 序列化
    bool Serialize(std::string &out_string)
    {
        Json::Value root;
        root["x"] = _x;
        root["y"] = _y;
        root["oper"] = _oper;

        // 固定的应用方式
        Json::StreamWriterBuilder str;
        std::unique_ptr<Json::StreamWriter> st(str.newStreamWriter());
        std::stringstream ss;
        st->write(root, &ss);
        out_string = ss.str();

        return true;
    }

    // 反序列化
    bool Deserialize(std::string &in_string)
    {
        Json::Value root;
        Json::Reader reader;
        bool parsingJudge = reader.parse(in_string, root);

        if (!parsingJudge)
        {
            std::cout << "Failed to parse JSON: " << reader.getFormatedErrorMessages() << std::endl;
            return false;
        }

        _x = root["x"].asInt();
        _y = root["y"].asInt();
        _oper = root["oper"].asInt();

        return true;
    }

    // 打印数据
    void Print()
    {
        std::cout << _x << std::endl;
        std::cout << _oper << std::endl;
        std::cout << _y << std::endl;
    }

    char Oper() const
    {
        return _oper;
    }

    int X() const
    {
        return _x;
    }

    int Y() const
    {
        return _y;
    }
};

class Response
{
private:
    int _result;
    int _code; // 错误码
public:
    Response() : _result(0), _code(0)
    {
    }
    Response(int result, int code) : _result(result), _code(code)
    {
    }
    // 序列化
    bool Serialize(std::string &out_string)
    {
        Json::Value root;
        root["result"] = _result;
        root["code"] = _code;

        // 固定的应用方式
        Json::StreamWriterBuilder str;
        std::unique_ptr<Json::StreamWriter> st(str.newStreamWriter());
        std::stringstream ss;
        st->write(root, &ss);
        out_string = ss.str();

        return true;
    }

    // 反序列化
    bool Deserialize(std::string &in_string)
    {
        Json::Value root;
        Json::Reader reader;
        bool parsingJudge = reader.parse(in_string, root);

        if (!parsingJudge)
        {
            std::cout << "Failed to parse JSON: " << reader.getFormatedErrorMessages() << std::endl;
            return false;
        }

        _result = root["result"].asInt();
        _code = root["code"].asInt();
        return true;
    }

    int Result() const { return _result; }
    int Code() const { return _code; }
    void SetResult(int res) { _result = res; }
    void SetCode(int c) { _code = c; }
};

// 序列和反序列方法